package com.problem.leetcode;

/**
 * 解法:不考虑通用性的话，使用两个变量依次保存第一大和第二大的数字，如果考虑通用性就需要使用优先级队列，不过如果 第k大的数字 k > n /2 就要将题目转变成第(n-k)小的数字
 *
 * @author didi
 * @DESCRIPTION
 * @create 2019-04-28 13:31
 **/

public class Problem414 {
    public int thirdMax(int[] nums) {
        long minVal   = (long) Integer.MIN_VALUE - 1;
        long maxFirst = minVal, maxSecond = minVal, maxThird = minVal;
        for (int num : nums) {
            if (num > maxFirst) {
                maxThird = maxSecond;
                maxSecond = maxFirst;
                maxFirst = num;
            } else if (num < maxFirst && num > maxSecond) {
                maxThird = maxSecond;
                maxSecond = num;
            } else if (num < maxSecond && num > maxThird) {
                maxThird = num;
            }
        }
        return (int) (maxThird == minVal ? maxFirst : maxThird);
    }
}
